package com.cloudcc.mobile.db;

import android.content.Context;

import com.cloudcc.mobile.entity.chat.Event;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;

/**
 * 任务数据库
 * 
 * @author Administrator
 * 
 */
public class EventDB {
	private Context context;
	private DbUtils db;

	public EventDB(Context context, String dbName) {
		db = DbUtils.create(context, dbName);
		this.context = context;
	}

	/**
	 * 保存数据库
	 * 
	 * @param cls
	 * @throws DbException
	 */
	public void saveEvent(Event entity) throws DbException {
		db.save(entity);
	}

	/**
	 * 删除所有消息
	 * 
	 * @param <T>
	 * @throws DbException
	 */
	public void deleteEventAll() throws DbException {
		db.deleteAll(Event.class);
	}

	/**
	 * 获取系统新消息数量
	 * 
	 * @param <T>
	 * @throws DbException
	 */

	public long getNewEventNum() throws DbException {
		if (isExit()) {
			long num = db.count(Selector.from(Event.class).where("isNew", "=",
					"1"));
			return num;
		} else {
			return 0;
		}
	}

	/**
	 * 判别是否存在
	 * 
	 * @return
	 * @throws DbException
	 */
	public boolean isExit() throws DbException {
		boolean istable = db.tableIsExist(Event.class);
		if (istable) {
			long a = db.count(Event.class);
			if (a > 0) {
				return true;
			} else {
				return false;
			}
		}
		return false;
	}
}
